//
//  BKNavigationController.m
//  AFNetworking
//
//  Created by baboy on 2019/2/16.
//

#import "BKNavigationController.h"
#import "Theme.h"
#import "ThemeFactory.h"
#import "BKViewController.h"
#import "UINavigationController+x.h"

@implementation BKNavigationController
- (id) initWithRootViewController:(UIViewController *)rootViewController{
    if (self = [super initWithRootViewController:rootViewController]) {
        [self setNavigationBarBackgroundImage:THEME_NAV_BAR_BG_IMAGE];
    }
    return self;
}
- (id) init{
    if (self = [super init]) {
        [self setNavigationBarBackgroundImage:THEME_NAV_BAR_BG_IMAGE];
    }
    return self;
}
- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self setNavigationBarBackgroundImage:THEME_NAV_BAR_BG_IMAGE];
    }
    return self;
}
- (void)setNavBgColor:(UIColor *)color{
    [self.navigationBar setTintColor:color];
}
- (void)popViewController{
    [self popViewControllerAnimated:YES];
}
- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    [super pushViewController:viewController animated:animated];
    if ([[self viewControllers] count] <= 1) {
        return;
    }
    if ([viewController isKindOfClass:[BKViewController class]] && !viewController.navigationController.navigationBar.hidden) {
        viewController.navigationItem.leftBarButtonItem = [ThemeFactory navBarButtonForKey:@"navigationbar-back-button" withTarget:self action:@selector(popViewController:)];
    }
}
- (BOOL)shouldAutorotate{
    UIViewController *vc = self.topViewController;
    if ([vc respondsToSelector:@selector(shouldAutorotate)]) {
        return [vc shouldAutorotate];
    }
    return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    UIViewController *vc = self.topViewController;
    if ([vc respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [vc supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}
@end
